package it.gius.pe.mains;

import java.util.concurrent.Semaphore;

import org.jbox2d.common.Vec2;

import it.gius.pe.Shape.BadShapeException;
import it.gius.pe.Shape.Ellipse;
import it.gius.pe.Shape.Polygon;
import it.gius.processing.gjkTest.GjkFinalApplet;
import it.gius.processing.gjkTest.MinkDiffApplet;
import it.gius.processing.util.PAppletManager;
import it.gius.processing.util.ProcessingGraphicException;

public class MainMovingTest {

	
	public static final int sizeX = 400;
	public static final int sizeY = 400;
	
	static Polygon poly1,poly2,poly3 = null;
	
	static Ellipse ellipse;

	public static void main(String args[])
	{
		initShapes();
		
		Polygon[] arrayPoly = new Polygon[3];
		arrayPoly[0] = poly1;
		arrayPoly[1] = poly2;
		arrayPoly[2] = poly3;
		
		Ellipse[] ellipseArray = new Ellipse[1];
		ellipseArray[0] = ellipse;
		
		
		Semaphore mutex = new Semaphore(1);
		
		GjkFinalApplet gjkApplet = new GjkFinalApplet(arrayPoly,ellipseArray,mutex);
		MinkDiffApplet minkApplet = new MinkDiffApplet(arrayPoly,mutex);
		boolean exitOnLastDisposed = false;
		boolean manualStart = true;
		PAppletManager manager = new PAppletManager(exitOnLastDisposed,manualStart);


		try {
			mutex = manager.startAndAddApplet(new String[]{"--location=150,200"},
					gjkApplet,PAppletManager.DISPOSE_ALL);
			
			manager.startAndAddApplet(new String[]{"--location=605,200"},
					minkApplet,PAppletManager.DISPOSE_ONLY_ME);
			
		} catch (ProcessingGraphicException e) {
			e.printStackTrace();
			System.exit(1);
		}
		
		try {
			if(mutex!= null)
				mutex.acquire();
			
			double mediumEP = gjkApplet.getMediumIterationEP();
			double mediumPP = gjkApplet.getMediumIterationPP();
			
			System.out.println("medium iteration ellipse polygon: " + mediumEP);
			System.out.println("medium iteration polygon polygon: " + mediumPP);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}


	}
	
	
	private static void initShapes()
	{
		poly1 = new Polygon();

		//convex polygon
		poly1.addVertex(new Vec2(20,10));
		poly1.addVertex(new Vec2(2,30));
		poly1.addVertex(new Vec2(40,40));
		poly1.addVertex(new Vec2(50,20));

		
		try {
			poly1.endPolygon();
		} catch (BadShapeException e) {
			e.printStackTrace();
			System.out.println("bad shape 1");
			poly1 = null;
		}
		
		poly1.setPosition(new Vec2(95,200));
		poly1.velocity.set(0.5f,0);	
		
		
		poly2 = new Polygon();

		//convex polygon
		poly2.addVertex(new Vec2(20,15));
		poly2.addVertex(new Vec2(12,50));
		poly2.addVertex(new Vec2(60,50));
		poly2.addVertex(new Vec2(50,20));

		
		try {
			poly2.endPolygon();
		} catch (BadShapeException e) {
			e.printStackTrace();
			System.out.println("bad shape 2");
			poly2 = null;
		}
		
		poly2.setPosition(new Vec2(300,200));
		poly2.velocity.set(-0.25f,0);	
		
		
		
		poly3 = new Polygon();

		//convex polygon
		poly3.addVertex(new Vec2(0,0));
		poly3.addVertex(new Vec2(0,20));
		poly3.addVertex(new Vec2(30,50));
		poly3.addVertex(new Vec2(50,50));
		poly3.addVertex(new Vec2(90,40));
		poly3.addVertex(new Vec2(75,20));

		
		try {
			poly3.endPolygon();
		} catch (BadShapeException e) {
			e.printStackTrace();
			System.out.println("bad shape 3");
			poly3 = null;
		}
		
		poly3.setPosition(new Vec2(250,100));
		poly2.velocity.set(-0.25f,0);	

		
		ellipse = new Ellipse(50,25,new Vec2(100,100),(float)Math.PI/3);


	}

}
